﻿using System;
using System.Data;
using System.Data.Common;
using System.Text;
/// <summary>
/// 从数据库里读取出的一篇文章的所有信息(作者、标题、正文、分类、存储日期等等)
/// </summary>
public class ArticleInfo
{
    public static int DEFAULT_PREVIEW_LENGTH = 200; // 200个中文字符
    public static string DEFAULT_PREVIEW_DOTS = "......"; // 预览 = 正文头 + '...' + 正文尾
    public static float DEFAULT_CUT_DOWN_RATIO = 0.618f; // 预览从此处截断

    public int ArticleID;
    public string Category;
    public string Tag;
    public string Author;
    public string Introduction;
    /// <summary>
    /// 文章正文部分，不含标题
    /// </summary>
    public string Content;
    public string Title;
    public DateTime CreateDate;

    private bool IsValid;
    private string ErrorInfo;

    private DBControl dbControl = new DBControl();

	public ArticleInfo()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        IsValid = false;
	}

    public ArticleInfo(DataSet data)
    {
        // 比较慢的一种实现方式
        try
        {
            DataTable dt = data.Tables[0];
            DataRow row = dt.Rows[0];
            dbControl.TryGetInt(row, "ArticleID", ref ArticleID);
            dbControl.TryGetString(row, "Category", ref Category);
            dbControl.TryGetString(row, "Tag", ref Tag);
            dbControl.TryGetString(row, "Author", ref Author);
            dbControl.TryGetString(row, "Introduction", ref Introduction);
            dbControl.TryGetString(row, "Content", ref Content);
            dbControl.TryGetString(row, "Title", ref Title);
            dbControl.TryGetDateTime(row, "CreateDate", ref CreateDate);

            IsValid = true;
        }
        catch (System.Exception e)
        {
            IsValid = false;
            ErrorInfo += e.ToString();
        }
    }

    public string GetContentForPreview()
    {
        string preview_content = "";
        int length = Content.Length;

        if (length < DEFAULT_PREVIEW_LENGTH)
        {
            preview_content = Content;
        }
        else
        {
            // 此处有可能会有1~若干个字符的误差，但不碍事
            int header_end = (int)(DEFAULT_CUT_DOWN_RATIO * DEFAULT_PREVIEW_LENGTH);
            int tail_length = DEFAULT_PREVIEW_LENGTH - header_end;
            int tail_start = length - tail_length;
            StringBuilder sb = new StringBuilder();
            sb.Append(Content.Substring(0, header_end));
            sb.Append(DEFAULT_PREVIEW_DOTS);
            sb.Append(Content.Substring(tail_start));
            preview_content = sb.ToString();
        }

        return preview_content;
    }

    public bool GetIsValid()
    {
        return IsValid;
    }

    public string GetErrorInfo()
    {
        return ErrorInfo;
    }

    public override string ToString()
    {
        string info = "";

        info += string.Format("ArticleID = {0}, Author = {1}, Category = {2}, Title = {3}, Preview = {4}, CreateDate = {5} , IsValid = {6}",
            ArticleID, Author, Category, Title, GetContentForPreview(), CreateDate, IsValid);

        if (IsValid == false)
        {
            info += ", error info = " + ErrorInfo.Substring(0) + "...";
        }

        return info;
    }
}
